package com.senbusi.shiro.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * json工具
 *
 * @author senbusi
 */
import java.io.IOException;

public class JSONUtils {

    private static final Logger logger = LoggerFactory.getLogger(JSONUtils.class);

    private JSONUtils() {
    }

    public static JSONObject parseJSONObject(String json) {
        try {
            return new JSONObject(json);
        } catch (Exception e) {
            logger.error("parse JSONObject error!", e);
        }
        return null;
    }

    public static JSONArray parseJSONArray(String jsonArray) {
        try {
            return new JSONArray(jsonArray);
        } catch (Exception e) {
            logger.error("parse parseJSONArray error!", e);
        }
        return null;
    }

    public static String getString(JSONObject obj, String name) {
        String value = "";
        try {
            value = obj.getString(name);
        } catch (Exception e) {
            logger.error("JSONObject get String value error!", e);
        }
        return value;
    }

    public static Long getLong(JSONObject obj, String name) {
        Long value = 0L;
        try {
            value = obj.getLong(name);
        } catch (Exception e) {
            logger.error("JSONObject get Long value error!", e);
        }
        return value;
    }

    public static JSONArray getJSONArray(JSONObject obj, String name) {
        try {
            return obj.getJSONArray(name);
        } catch (Exception e) {
            logger.error("JSONObject get JSONArray value error!", e);
        }
        return null;
    }

    public static String toJson(Object obj) {
        if (obj == null) {
            return "";
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            logger.error("JSONUtils to String error!", e);
        }
        return "";
    }

    public static <T> T fromJson(String json, Class<T> clazz) {
        if (StringUtils.isBlank(json)) return null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json, clazz);
        } catch (IOException e) {
            logger.error("JSONUtils fromJson error!", e);
        }
        return null;
    }


    public static <T> T fromJsonWithoutErr(String json, Class<T> clazz) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json, clazz);
        } catch (IOException ignored) {
        }
        return null;
    }

    /**
     * 取字符串值 调用toString方法
     *
     * @param v  对象
     * @param dv 默认值
     * @return
     */
    String strValue(Object v, String dv) {
        if (v != null && v instanceof String)
            return StringUtils.isBlank(v.toString()) ? dv : v.toString();
        return v == null ? dv : v.toString();
    }
}
